package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IStratrgyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedis;
    @Autowired
    private IStratrgyStatisVORedisService statisVORedisService;

    @GetMapping("get")
    public Object get(String id){

        return userInfoService.get(id);
    }

    @GetMapping("checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);

        return JsonResult.success();
    }

    @PostMapping("regist")
    public Object regist(String phone,String nickname,String password,
                         String rpassword,String verifyCode){
        //try {
            //注册信息
            userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
       // } catch (LogicException e) {
            //出问题之后给页面提示信息
            /*e.printStackTrace();
            return JsonResult.error(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);
        }catch (Exception ex){*/
            //这种抓取异常的方式存在问题:
            //给用户提示的异常跟系统的异常都在这个位置被抓取了
            //真正的要求,用户提示异常要返回,系统异常需要美化
            //此时需要操作区分,引出问题如何区分系统异常,用户异常
            //系统异常多样化,无法统一处理,用户异常可以自己控制,所以解决方法是自定义异常
            /*ex.printStackTrace();
            //出现问题时给页面的提示信息
            return JsonResult.defaultError();
        }*/
        return JsonResult.success();
    }

    @PostMapping("login")
    public Object login(String username,String password){
        //获取user
        UserInfo user= userInfoService.login(username,password);

        //获取token
        String token = userInfoRedis.setToken(user);
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);

        return JsonResult.success(map);
    }

    @RequireLogin
    @GetMapping("currentUser")
    public Object currentUser(HttpServletRequest request){

        //通过请求对象获取前端传过来的token
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedis.getUserByToken(token);
        return JsonResult.success(userInfo);
    }

    @GetMapping("strategies/favor")
    public Object strategiesFavor(String userId){

        return JsonResult.success(statisVORedisService.getSids(userId));
    }

}
